<?php
require_once('../../../../conf/config.php.inc');
require_once(SYBMAS_LIB_PATH.'/liblogin.php.inc');
require_once(SYBMAS_LAYOUT_PATH.'/layout.php.inc');
require_once(SYBMAS_LIB_PATH.'/libsybmas.php.inc');
require_once(SYBMAS_LAYOUT_PATH.'/menu.php.inc');
require_once('../../lib/libtablas.php.inc');
verificar_inicio_sesion();
?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<link rel="stylesheet" type="text/css"
	href="<?php echo LAYOUT_URI?>/sybmasstyle.css">
<script src="<?php echo LAYOUT_URI?>/jquery-1.2.1.min.js"
	type="text/javascript"></script>
<script src="<?php echo LAYOUT_URI?>/menu-collapsed.js"
	type="text/javascript"></script>
<link rel="stylesheet" type="text/css"
	href="<?php echo LAYOUT_URI?>/style.css" >
<link rel="stylesheet" type="text/css"
	href="<?php echo JAVASCRIPT_URI?>/tabs/tabs.css">
<script src="<?php echo JAVASCRIPT_URI?>/tabs/tabs.js"
	type="text/javascript"></script>
<title>.:: Sybmas - Crear tabla</title>
<!--[if lt IE 8]>
   <style type="text/css">
   li a {display:inline-block;}
   li a {display:block;}
   </style>
   <![endif]-->
</head>
<body>
<script src="<?php echo JAVASCRIPT_URI?>/tooltip/wz_tooltip.js"
	type="text/javascript"></script>
<center>
<div id="container">
<div id="header"><?php generar_header();?></div>
<div id="scene">
<div id="modulos">
<ul id="menu" style="width: 150px;">
<?php generar_menu();?>
</ul>
</div>
<?php 
$dbname=false;
foreach($_POST as $key => $value){
	$$key=$value;
	//echo "$key: $value <br>";
}
?>
<div id="title">Confirmacion de creaci&oacute;n de la tabla:<b><?php echo $tablename?></b></div>
<div id="main"> <?php
$cambio_bd_exitoso=true;
$sybuser_connection=start_default_sybuser_connection();
if($dbname){
	$cambio_bd_exitoso=sybase_select_db($dbname,$sybuser_connection);
}

if($cambio_bd_exitoso){
	
	$query="CREATE TABLE ";
	$query.="$tablename(";
	
	$firstLoop=true;	
	foreach($columnDefinition as $column){
		if(!$firstLoop){
			$query.=",";
		}
		
		$data=explode('|',$column);
		$colName=$data[0];
		$colType=$data[1];
		$colLength=$data[2];
		$colDefault=$data[3];
		$colNullable=$data[4];
		
		$query.="$colName $colType";
		$query.=!empty($colLength)?"($colLength)":"";
		$query.=!empty($colDefault)?" DEFAULT $colDefault":"";
		$query.=!empty($colNullable)?" $colNullable ":"";
		
		//$query.=$column;
		$firstLoop=false;
	}
	$query.=") ";
	
	$options1=array(
		'max_rows_per_page'=>$max_rows_per_page,
		'reservspacegap'=>$reservspacegap,
		'exp_row_size'=>$exp_row_size
	);
	
	$withOptions1=false;	
	foreach($options1 as $option){
		if(!empty($option)){
			$withOptions1=true;
			break;
		}
	}
	
	if($withOptions1 ){
		$query.="WITH ";
		$firstLoop=true;
		foreach($options1 as $option=>$value){
			if(empty($value))
				continue;			
			if(!$firstLoop)
				$query.=",";
			$firstLoop=false;	
			$query.="$option=$value";
		}
	}
	
	$query.=" ON '$segment'";	
	//echo $query;
	
	sybase_set_message_handler("void_msg_handler",$sybuser_connection);
	$result=@sybase_query($query,$sybuser_connection);
	sybase_set_message_handler("msg_handler",$sybuser_connection);
	
	if($result){
		echo "<h1>La tabla <b>$tablename</b> fue creada exitosamente</h1>";
		if(!empty($constraintDefinition)){
			foreach($constraintDefinition as $constraint){
				$query="ALTER TABLE $tablename ADD $constraint";
				
				sybase_set_message_handler("void_msg_handler",$sybuser_connection);
				$result=@sybase_query($query,$sybuser_connection);
				sybase_set_message_handler("msg_handler",$sybuser_connection);
			
				if($result){
					echo "<h3>Se agrego la restriccion <b>$constraint</b></h3>";
				}else{
					echo "<h3>Error al agregra la restriccion <b>$constraint</b></h3>";
					echo "<h4>".sybase_get_last_message()."</h4>";
				}		
			}	
		}
	}else{
		echo "<h1>Error al crear la tabla <b>$tablename</b></h1>";
		echo "<h4>".sybase_get_last_message()."</h4>";
	}
		
}//if($cambio_bd_exitoso)

//Cerrando conexiones a la base de datos
sybase_close($sybuser_connection);
?>

	<a href="../../tablas" >Aceptar</a>




</div>
<!-- main --></div>
<!-- scene --> <?php generar_footer();?></div>
</center>
</body>
</html>
